package com.pospi.pospihold;

import android.app.Activity;
import android.app.Application;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.database.sqlite.SQLiteDatabase;
import android.os.IBinder;
import android.support.multidex.MultiDex;
import android.util.Log;

import com.chinaums.mpos.aar.UmsPayManager;
import com.chinaums.mpos.service.IUmsMposService;
import com.lany.sp.SPHelper;
import com.pospi.pospihold.dto.CashierMsgDto;
import com.pospi.pospihold.greendao.DaoMaster;
import com.pospi.pospihold.greendao.DaoSession;
import com.tsy.sdk.myokhttp.MyOkHttp;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;

public class App extends Application {
    private static Context context;
    private static List<Activity> activities = new ArrayList<>();
    private static CashierMsgDto cashierMsgDto;
    private static App app;

    private SQLiteDatabase db;
    private DaoMaster daoMaster;
    private DaoSession daoSession;
    private MyOkHttp myOkHttp;
    private IUmsMposService mposAidlService;

    public static boolean IsPrinter;//是否使用打印机呢

    public static List<Activity> getActivities() {
        return activities;
    }

    public IUmsMposService getMposAidlService() {
        return mposAidlService;
    }

    private ServiceConnection mConnection = new ServiceConnection() {
        public void onServiceConnected(ComponentName className, IBinder service) {
            mposAidlService = IUmsMposService.Stub.asInterface(service);
            Log.i("service", "已连接-----"+mposAidlService);

        }

        public void onServiceDisconnected(ComponentName className) {
            mposAidlService = null;
            Log.i("service", "已断开-----"+mposAidlService);
        }
    };
    /**
     * 竖屏版本插件的绑定
     */
    private void bindUmsMposServiceForPhone() {
        Intent intent = new Intent();
        // ******请注意 横屏版 与 竖屏版 的classname有区别！！
        intent.setClassName("com.chinaums.mposplugin", "com.chinaums.mpos.service.MposService");
        bindService(intent, mConnection, Context.BIND_AUTO_CREATE);
    }

    public static void killAllActivities() {
        for (Activity activity : activities) {
            activity.finish();
        }
    }
    //初始化greendao
    private void initDataBase() {
        DaoMaster.DevOpenHelper openHelper = new DaoMaster.DevOpenHelper(this, "pospi_db", null);
        db = openHelper.getWritableDatabase();
        daoMaster = new DaoMaster(db);
        daoSession = daoMaster.newSession();
    }
    public DaoSession getDaoSession() {
        return daoSession;
    }

    public static Context getContext() {
        return context;
    }
    public static CashierMsgDto getCashierMsgDto() {
        return cashierMsgDto;
    }

    public static void setCashierMsgDto(CashierMsgDto cashierMsgDto) {
        App.cashierMsgDto = cashierMsgDto;
    }

    @Override
    public void onCreate() {
        super.onCreate();
        app = this;
        //初始化网络请求
        OkHttpClient okHttpClient = new OkHttpClient.Builder()
                .connectTimeout(15000L, TimeUnit.MILLISECONDS)
                .readTimeout(15000L, TimeUnit.MILLISECONDS).build();
        myOkHttp = new MyOkHttp(okHttpClient);
        //初始化sharedprefece
        SPHelper.getInstance().init(this, com.lany.sp.BuildConfig.DEBUG);
        initDataBase();
//        bindUmsMposServiceForPhone();
        UmsPayManager.getInstance().init(this);
    }

    public static App getInstance(){
        return app;
    }
    public MyOkHttp getMyOkHttp() {
        return myOkHttp;
    }

    @Override
    protected void attachBaseContext(Context base) {
        super.attachBaseContext(base);
        MultiDex.install(this);
    }
}
